<template>
    <div>
        <cus-modal ref="modal" width="400" :title="isAdmin == 1 ? '用户交接' : '重置密码'" @on-visible-change="onVisibleChange" @onOk="onOk">
            <Form ref="formValidate" :model="form" :rules="ruleValidate" :label-width="100">
                <FormItem label="用户交接"  prop="loginName" v-if="isAdmin == 1" :rules="{required: true, message: '交接用户不能为空', trigger: 'blur'}">
                    <cus-autoComplete :value="form"  key-label="loginName" key-value="id" placeholder="请输入用户名称选择用户"
                        return-label="data" @onChange="changeUserOption" :cusParams="{ unitCode: vuex_user.base.unitCode,status: 1}"
                        fncLabel="loginName" pageSize="size" currentPage="current" :fnc="getUserListApi"></cus-autoComplete>
                </FormItem>
                <FormItem label="新密码" prop="password" v-if="isAdmin == 0">
                    <Input v-model.trim="form.password" type="password"    style="width: 200px" placeholder="请输入密码"></Input>
                </FormItem>
                <FormItem label="确认密码" prop="passwordNotarize" v-if="isAdmin == 0" >
                    <Input v-model.trim="form.passwordNotarize"  type="password"  style="width: 200px"  placeholder="请确认密码"></Input>
                </FormItem>
            </Form>
            <Spin size="large" fix v-if="loading"></Spin>
        </cus-modal>
    </div>
</template>    
  
<script>
import { getUserList, handoverUser,resetUserPassword} from '@/api/organ/system'

const form = {
    id: '',
    name: '',
    loginName: '',
    phoneNumber: '',
    postName: '', //职务
    unitName: '',//单位
    password: '',
    passwordNotarize: '',//确认密码
    descr: '',
    roleNameList: [], //角色名称
    roleIdList: [], //角色id

}

export default {
    name: "handleModal",
    data() {
        return {
            loading: false,
            form: form,
            isAdmin: '', //是否为交接
            userList: [],
            roleList: [],
            ruleValidate: {
                password: [
                {required: true, min:5,  message: '密码长度必须大于或等于5位', trigger: 'change'},
                {validator: (rule, value, callback) => {
            let reg = new RegExp("[\\u4E00-\\u9FFF]+","g")
            if (reg.test(value)) {
                    callback(new Error('密码不能含有中文'));
                } else {
                    callback();
                }
        }, trigger: 'change'}
                ]

            },
            getUserListApi: getUserList,
            pages: {
                page: 1,
                pageSize: 20,
                pageTotal: 0
            },
        }
    },
    mounted() {
        this.form = this.$cu.deepClone(form);
    },
    methods: {
        onVisibleChange(v) {
            this.$refs['formValidate'].resetFields();
            if (!v) this.form = this.$cu.deepClone(form)
            
        },
        show() {
            this.$refs['modal'].show = true;
            console.log(this.form.id);
        },
        onOk() {
            this.$refs['formValidate'].validate(valid => {
                if (valid) {
                    let contentA = '确认重置密码'
                    let contentB = '确认将当前帐号交接给' + this.form.loginName
                    this.$cu.dialog({
                    content: this.isAdmin == 0 ? contentA : contentB
             }).then(() => {
            if(this.isAdmin == 0) {
                      if(this.form.password == this.form.passwordNotarize) {
                        // 修改密码
                        this.getResetUserPassword()
                      }else {
                        this.$Message.error('两次输入的密码不一致')
                      }
                    }else {
                        this.getHandoverUser()
                    }
        })      
                }
            })
        },
        // 改变用户
        changeUserOption(val) {
            console.log(111111111111111111111);
            this.$refs['formValidate'].resetFields();
            this.form.id = val.id
            this.form.loginName = val.loginName
           
        },
       
        // 重置密码
        getResetUserPassword() {
            console.log('用户id',this.form.id);
            this.form.loading = true
            return new Promise(resolve => {
                resetUserPassword({
                    userId: this.form.id,
                    password: this.form.password
                }).then(res => {
                    if (res.code === 1) {
                        this.form.loading = false
                        this.$Message.success('重置密码成功');
                        this.$refs['modal'].show = false;
                        this.$parent.onInit();
                        resolve();
                    }

                })
            })
        },
        //   管理员交接
        getHandoverUser() {
            this.form.loading = true
            return new Promise(resolve => {
                handoverUser({
                    toUserId: this.form.id,
                }).then(res => {
                    if (res.code === 1) {
                        this.form.loading = false
                        this.$Message.success('交接成功');
                  this.$refs['modal'].show = false;
                  this.$parent.onInit();
                        resolve();
                    }

                })
            })
        },
    }
}
</script>
  
<style scoped lang="less">
.permission {
    height: 300px;
    width: 500px;
    border: 1px solid #dcdee2;
    overflow-y: auto;
    border-radius: 2px;
    padding: 0 0 0 10px;
}
</style>
  